package com.opass.boot.core.constants;

import org.springframework.http.HttpStatus;

/**
 * 自定义返回码和Http状态码
 * 其他业务码,直接通过BaseFailException抛出即可
 *
 * @author admin
 */

public enum ResultCode {
    // 成功
    OK(20000, HttpStatus.OK, "success"),
    // 错误提示:2000
    FAILED(20001, HttpStatus.OK, "failed"),

    BAD_REQUEST(4000, HttpStatus.BAD_REQUEST, "无效的请求:缺少参数或类型不匹配!"),
    SIGNATURE_DENIED(4001, HttpStatus.BAD_REQUEST, "签名错误!"),

    UNAUTHORIZED(4010, HttpStatus.UNAUTHORIZED, "认证失败!"),
    BAD_CREDENTIALS(4011, HttpStatus.OK, "账户名与密码不匹配!"),
    ACCOUNT_DISABLED(4012, HttpStatus.OK, "账号已过期!"),
    ACCOUNT_EXPIRED(4013, HttpStatus.OK, "账号已过期!"),
    ACCOUNT_CREDENTIALS_EXPIRED(4014, HttpStatus.OK, "凭证已过期!"),
    ACCOUNT_LOCKED(4015, HttpStatus.OK, "账号已被锁定!"),

    ACCESS_DENIED(4031, HttpStatus.FORBIDDEN, "访问权限不足!"),
    ACCESS_DENIED_BLACK_LIMITED(4032, HttpStatus.FORBIDDEN, "IP或域名拒绝访问!"),
    ACCESS_DENIED_WHITE_LIMITED(4033, HttpStatus.FORBIDDEN, "IP或域名不在白名单内!"),

    NOT_FOUND(4040, HttpStatus.NOT_FOUND, "无效的访问地址！"),
    METHOD_NOT_ALLOWED(4050, HttpStatus.METHOD_NOT_ALLOWED, "请求方式不支持!"),
    MEDIA_TYPE_NOT_ACCEPTABLE(4060, HttpStatus.NOT_ACCEPTABLE, "媒体类型不支持!"),
    TOO_MANY_REQUESTS(4290, HttpStatus.TOO_MANY_REQUESTS, "访问太过频繁，请稍后再试!"),

    SYSTEM_ERROR(5000, HttpStatus.INTERNAL_SERVER_ERROR, "系统繁忙,请稍后再试!"),
    SERVICE_UNAVAILABLE(5030, HttpStatus.SERVICE_UNAVAILABLE, "服务暂时无法访问!"),
    GATEWAY_TIMEOUT(5040, HttpStatus.GATEWAY_TIMEOUT, "服务请求超时!");


    private int code;
    private String message;
    private HttpStatus httpStatus;

    ResultCode() {
    }

    ResultCode(int code, HttpStatus httpStatus, String message) {
        this.code = code;
        this.message = message;
        this.httpStatus = httpStatus;
    }

    public static ResultCode getResultEnum(HttpStatus httpStatus) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getHttpStatus().value() == httpStatus.value()) {
                return type;
            }
        }
        return SYSTEM_ERROR;
    }

    public static ResultCode valueOfCode(int code) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return SYSTEM_ERROR;
    }

    public static ResultCode valueOfMessage(String message) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getMessage().equals(message)) {
                return type;
            }
        }
        return SYSTEM_ERROR;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }


    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
